home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 9 / CDACTUAL9.iso / share / Dos / VARIOS / pascal / DELPHI.SWG / 0019_Associate a string with a component.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1996-02-21  |  2.9 KB  |  86 lines

  1. {
  2. Q:  Is there a way to associate a string with each component?
  3.  
  4. A:  Since the Tag property is a longint, you can type cast it
  5. as a Pointer or PChar.  So, you can basically store a pointer
  6. to a record by using the Tag property.
  7.  
  8. Note:  You're not going to be able to store the string, or
  9. pointer rather, at design time. This is something you'll have
  10. to do at run time. Take a look at this example:
  11. }
  12.  var
  13.   i: integer;
  14.  begin
  15.    for i := 0 to ComponentCount - 1 do
  16.  
  17.      if Components[i] is TEdit then
  18.        Components[i].Tag := LongInt(NewStr('Hello '+IntToStr(i)));
  19.  end;
  20.  
  21. Here, I loop through the components on the form.  If the 
  22. component is a TEdit, I assign a pointer to a string to its Tag 
  23. property.  The NewStr function returns a PString (pointer to a 
  24. string).  A pointer is basically the same as a longint or 
  25. better, occupies the same number of bytes in memory. Therefore, 
  26. you can type cast the return value of NewStr as a LongInt and 
  27. store it in the Tag property of the TEdit component.  Keep in 
  28. mind that this could have been a pointer to an entire record.  
  29. Now I'll use that value:
  30.  
  31.  var
  32.   i: integer;
  33.  begin
  34.    for i := 0 to ComponentCount - 1 do
  35.      if Components[i] is TEdit then begin
  36.        TEdit(Components[i]).Text := PString(Components[i].Tag)^;
  37.        DisposeStr(PString(Components[i].Tag));
  38.      end;
  39.  end;
  40.  
  41. Here, again I loop through the components and work on only the 
  42. TEdits.  This time, I extract the value of the component's Tag 
  43. property by typecasting it as a PString (Pointer to a string) 
  44. and assigning that value to the TEdit's Text property. Of 
  45. course, I must dereference it with the caret (^) symbol.  Once 
  46. I do that, I dispose of the string stored in the edit 
  47. component.  Important note: if you store anything in the 
  48. TEdit's Tag property as a pointer, you are responsible for 
  49. disposing of it also.
  50.  
  51. FYI, Since Delphi objects are really pointers to class 
  52. instances, you can also store objects in the Tag property. As 
  53. long as you remember to Free them.
  54.  
  55. Three methods spring to mind to use Tags to access strings that 
  56. persist from app to app.
  57.  
  58. 1.  If your strings stay the same forever, create a string
  59. resource in Resource Workshop (or equiv) and use the Tags as 
  60. indexes into your string resource.
  61.  
  62. 2.  Use TIniFile and create a section for your strings, and 
  63. give each string a name with number so that your ini file has a 
  64. section like this:
  65.  
  66. [strings]
  67. string1=Aristotle
  68. string2=Plato
  69. string3=Well this is Delphi, after all
  70.  
  71. Then you can fetch them back out this way:
  72.  
  73.   var s1: string;
  74.   ...
  75.   s1 := IniFile1.ReadString('strings', 'string'+IntToStr(Tag), '');
  76.  
  77. 3.  Put your strings into a file, with each followed by a
  78. carriage return.  Read them into a TStringList.  Then your Tags
  79. become an index into this stringlist:
  80.  
  81.   StringList1.LoadFromFile('slist.txt');
  82.   ...
  83.   s1 := StringList1[Tag];
  84.  
  85. Given the way Delphi is set up, I think the inifile method is easiest.
  86.